package tank01;

import java.awt.*;
import java.util.Random;

/**
 * @Auther: xiaofei
 * @Date: 2021/7/1 - 07 - 01 - 10:32
 * @Description: tank01
 * @version: 1.0
 */
//坦克
public class Tank {
    int x,y;
    Dir dir = Dir.DOWN;
    private static final int SPEED = 1;

    //计算坦克的宽度和高度
    public static int WIDTH = ResouceMgr.goodTankU.getWidth();
    public static int HEIGHT = ResouceMgr.goodTankU.getHeight();

    Rectangle rect = new Rectangle();

    private boolean moving = true;
    private boolean living = true;

    Group group = Group.BAD;
    TankFrame tf = null;

    private Random random = new Random();


    public Group getGroup(){
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isMoving(){
        return moving;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

//    public Tank() {
//    }

    public Tank(int x, int y, Dir dir,Group group,TankFrame tf) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tf = tf;
    }

    public void paint(Graphics g){
        if(!living) tf.tanks.remove(this);
        switch (dir){
            case LEFT:
                g.drawImage(ResouceMgr.goodTankL,x,y,null);
                x -= SPEED;
                break;
            case RIGHT:
                g.drawImage(ResouceMgr.goodTankR,x,y,null);
                //x += SPEED;
                break;
            case UP:
                g.drawImage(ResouceMgr.goodTankU,x,y,null);
                //y -= SPEED;
                break;
            case DOWN:
                g.drawImage(ResouceMgr.goodTankD,x,y,null);
                //y += SPEED;
                break;
        }
        Color c = g.getColor();
//        g.setColor(Color.YELLOW);
//        //建立一个矩阵
//        g.fillRect(x,y,50,50);
//        g.setColor(c);
        move();
    }

    private void move() {
        if(!moving) return;
        //根据按键确定坦克移动方向
        switch (dir){
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }

        if (this.group == Group.BAD && random.nextInt(100) > 95){
            this.fire();
            randomDir();
        }
        boundsCheck();

        //update rect
        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    //边界检测
    private void boundsCheck() {
        if (this.x < 0) x = 0;
        if (this.y < 30) x = 30;
        if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH)
            x = TankFrame.GAME_WIDTH - Tank.WIDTH;
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT)
            x = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
    }

    //敌方坦克随机开火
    private void randomDir(){
        this.dir = Dir.values()[random.nextInt(4)];
    }

    //坦克开火
    public void fire(){
        int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
        int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
        tf.bullets.add(new Bullet(bX, bY, this.dir,this.group, this.tf));
    }

    public void die(){
        this.living = false;
    }
}
